package com.哈希表;


import java.util.*;

public class RandomizedSet {
    List<Integer> set;
    /** Initialize your data structure here. */
    public RandomizedSet() {
        set = new ArrayList<>();
    }

    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    public boolean insert(int val) {
        Boolean flag = true;
        if(set.contains(val)){
            flag = false;
        }
        else{
            set.add(val);
            flag = true;
        }
        return flag;

    }

    /** Removes a value from the set. Returns true if the set contained the specified element. */
    public boolean remove(int val) {
        Boolean flag = true;
        if(set.contains(val)){
           int i = set.indexOf(val);
            set.remove(i);

            flag = true;
        }
        else{
            flag = false;
        }
        return flag;
    }

    /** Get a random element from the set. */
    public int getRandom() {
        if(set.size()==0){
            return -1;
        }
      int i = new Random().nextInt(set.size());
        return set.get(i);
    }

    public static void main(String[] args) {
        new LinkedList<>();
        List<Integer> list = new ArrayList<>();
        list.add(0,1);
        System.out.println(list.get(0));
        // 初始化一个空的集合。
        RandomizedSet randomSet = new RandomizedSet();
        while (true){

            String s  = new Scanner(System.in).next();
            int i = 0;
            switch (s){
                case "ins":
                     i = new Scanner(System.in).nextInt();
                    System.out.println(randomSet.insert(i));
                    break;
                case"del":
                     i = new Scanner(System.in).nextInt();
                    System.out.println(randomSet.remove(i));
                    break;
                case "get":
                    System.out.println(randomSet.getRandom());
                    break;
                case "all":
                    randomSet.set.forEach(x->{
                        System.out.print(x+"  ");
                    });
                    break;
                case "end":
                    System.exit(0);
            }
        }

    }
}
